/*
 * @Author: zhangwei 1405298045@qq.com
 * @Date: 2022-03-03 16:39:52
 * @LastEditTime: 2022-03-11 10:41:08
 * @LastEditors: zhangwei 1405298045@qq.com
 * @Description:
 * @FilePath: /ll-fe-mp-dyfx/src/utils/modules/uniApi.js
 *
 */
import Store from '@/store';

/**
 * @returns 获取 系统信息
 */
export const getSystemInfo = () => {
	const systemInfo = Store.getters['getSystemInfo'] || {};

	if (Object.keys(systemInfo).length) {
		return systemInfo;
	}

	const newSystemInfo = uni.getSystemInfoSync();
	Store.commit('changeSystemInfo', newSystemInfo);

	return newSystemInfo;
};

/**
 * 判断当前机型是 ios 还是 andriod
 */
export const isIos = () => {
	const { platform } = getSystemInfo();

	if (platform === 'ios') {
		return true;
	}

	return false;
};

/**
 * 获取当前的地理位置
 */
export const getLocation = () => {
	return new Promise((resolve, reject) => {
		uni.getLocation({
			type: 'wgs84',
			geocode: true,
			success: (res) => {
				const { latitude, longitude } = res;
				resolve({
					latitude,
					longitude
				});
			},
			fail: (e) => {
				resolve();
			}
		});
	});
};

/**
 * 获取用户授权项
 * @returns
 */
export const openSetting = () => {
	return new Promise((resolve, reject) => {
		// #ifdef MP-WEIXIN
		uni.openSetting({
			success: (res) => {
				const { authSetting } = res;
				resolve({
					userLocation: authSetting['scope.userLocation'],
					writePhotosAlbum: authSetting['scope.writePhotosAlbum']
				});
			}
		});
		// #endif
	});
};
